package org.finmanager.core;

import javax.swing.*;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static org.finmanager.core.Constants.ITEM_FILTER_UI_NAME;
import static org.finmanager.core.Constants.ITEM_FILTER_UI_WIDTH;
import static org.finmanager.core.Constants.ITEM_FILTER_UI_HEIGHT;
import static org.finmanager.core.Constants.ITEM_FILTER_UI_RESIZABLE;

/**
 * Created by Kelvin on 3/4/2014.
 */

//ALL items are shown in TABLE form in the budget function

//There is a "filter" button on the right top corner
//A window will be popped up
//Users may view SELECTED CATEGORIES items instead of ALL items in the table
//This is the UI for that small window
public class ItemFilterUI {
	private JPanel mainPanel = new JPanel();

	private JFrame frame;

	private JTable table;

	public ItemFilterUI(CategoryManagerController categoryManagerController, ActionListener listener) {
		table = new JTable(categoryManagerController.getCategoryModel().getCategoryTableModel());

		TableColumn column;
		for (int i = 0; i < table.getColumnCount(); ++i) {
			column = table.getColumnModel().getColumn(i);
			if (i == CategoryTableModel.NAME)
				column.setPreferredWidth(150);
			if (i == CategoryTableModel.FAVOURITE)
				table.removeColumn(column);
		}

		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.DARK_GRAY));

		table.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		table.setColumnSelectionAllowed(false);

		table.getTableHeader().setReorderingAllowed(false);
		table.getTableHeader().setResizingAllowed(false);

		table.setShowGrid(false);
		table.setIntercellSpacing(new Dimension(0,0));

		JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		JButton applyFilterButton = new JButton("Apply");
		JButton resetFilterButton = new JButton("Reset");
		JButton cancelButton = new JButton("Cancel");

		applyFilterButton.setActionCommand(ItemManagerController.APPLY_FILTER);
		applyFilterButton.addActionListener(listener);
		applyFilterButton.addActionListener(new CloseWindowListener());
		resetFilterButton.setActionCommand(ItemManagerController.RESET_FILTER);
		resetFilterButton.addActionListener(listener);
		resetFilterButton.addActionListener(new CloseWindowListener());
		cancelButton.addActionListener(new CloseWindowListener());

		buttonPanel.add(applyFilterButton);
		buttonPanel.add(resetFilterButton);
		buttonPanel.add(cancelButton);

		getMainComponent().setLayout(new BorderLayout(0, 0));
		getMainComponent().add(scrollPane, BorderLayout.CENTER);
		getMainComponent().add(buttonPanel, BorderLayout.SOUTH);

		createAndShowGUI(categoryManagerController.getItemManagerController().getUIMainComponent());

		updateRowHeights();
	}

	private void createAndShowGUI(JComponent relative) {
		frame = new JFrame(ITEM_FILTER_UI_NAME);
		frame.setSize(new Dimension(ITEM_FILTER_UI_WIDTH, ITEM_FILTER_UI_HEIGHT));
		frame.setLocationRelativeTo(relative);
		frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		frame.setResizable(ITEM_FILTER_UI_RESIZABLE);

		frame.add(getMainComponent());

		frame.setVisible(true);
	}

	private void updateRowHeights()
	{
		for (int row = 0; row < table.getRowCount(); row++)
		{
			int rowHeight = table.getRowHeight();

			for (int column = 0; column < table.getColumnCount(); column++)
			{
				Component comp = table.prepareRenderer(table.getCellRenderer(row, column), row, column);
				rowHeight = Math.max(rowHeight, comp.getPreferredSize().height);
			}

			table.setRowHeight(row, rowHeight);
		}
	}

	public JComponent getMainComponent() {
		return mainPanel;
	}

	private class CloseWindowListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			frame.dispose();
		}
	}
}
